<?php

class administration_Controller extends Controller {
    
    public function _default($params) {
        $view = new administration_View(new Module((int)$params['module']));
        $view->createView('menu',$params);
        return $view;
    }
    
    public function accounts($params) {
        $view = new administration_View(new Module((int)$params['module']));
        $view->createView('accounts',$params);
        return $view;
    }
    
    public function rights($params) {
        $view = new administration_View(new Module((int)$params['module']));
        $view->createView('rights',$params);
        return $view;
    }
    
    public function createUser($params) {
        $view = new administration_View(new Module((int)$params['module']));
        
        if(isset($_POST) && count($_POST)) {
            //createUser form sent
            $user = new AnyUser(0);
            $user->setUserData($_POST);
            $createUser = true;
            if(!isset($_POST['password']) || !isset($_POST['password2']) || $_POST['password'] != $_POST['password2']) {
                    $createUser = false;
                    $err = Error::instance();
                    $err->add("Hasło w obu polach musi być identyczne.");
            }
            if($createUser) {
                $uid = $user->create();
            } else {
                $uid = 0;
            }
            
            if($uid > 0) {
                $params['uid'] = $uid;
                $view->createView('createUserSuccess',$params);
            } else {
                $view->createView('createUserError',array_merge($params,array('userInfo'=>$_POST)));
            }
        }
        else {
            $view->createView('createUser', $params);
        }
        
        return $view;
    }
    
    public function delUser($params) {
        $view = new administration_View(new Module((int)$params['module']));
        if(isset($_POST) && count($_POST)) {
            $user = new AnyUser((int)$_POST['uid']);
            if($user->deleteUserFromDB()) {
                $view->createView('deleteUserSuccess', array_merge($params,array('uid'=>$_POST['uid'],'details'=>$_POST['details'])));
            } else {
                $view->createView('deleteUserError', array_merge($params,array('uid'=>$_POST['uid'],'details'=>$_POST['details'])));
            }
        } else {
            $view->createView('deleteUser',$params);
        }
        return $view;
    }
    
    public function editUser($params) {
        $view = new administration_View(new Module((int)$params['module']));
        if(isset($_POST['commit']) && isset($_POST['uid'])) {
            $user = new AnyUser((int)$_POST['uid']);
            $user->setUserData($_POST);
            $updateUser = true;
            if($_POST['password2'] != $_POST['password']) {
                    $updateUser = false;
                    $err = Error::instance();
                    $err->add("Podane hasła muszą się zgadzać!");
            }
            if($updateUser) {
                $updateUser = $user->updateUserInfoInDB();
            }
            if($updateUser) {
                $view->createView('editUserSuccess', array_merge($params,array('uid'=>$_POST['uid'])));
            } else {
                $view->createView('editUserError', array_merge($params,array('uid'=>$_POST['uid'])));
            }
        } else {
            $view->createView('editUser',$params);
        }
        return $view;
    }
    
    public function addGroup($params) {
        $view = new administration_View(new Module((int)$params['module']));
        if(isset($_POST['name'])) {
            if(strlen($_POST['name']) >= 3) {
                $group = new Group();
                $group->name = $_POST['name'];
                if(!($gid = $group->create())) {
                    $view->createView('addGroupError', $params);
                } else {
                    $view->createView('addGroupSuccess', array_merge($params,array('gid'=>$gid)));
                }
            } else {
                $err = Error::instance();
                $err->add("Nazwa grupy musi składać się z przynajmniej 3 znaków");
                $view->createView('addGroupError', $params);
            }
        } else {
            $view->createView('addGroup', $params);
        }
        return $view;
    }
    
    public function delGroup($params) {
        $view = new administration_View(new Module((int)$params['module']));
        if((isset($_POST['gid']))) {
            $group = new Group((int)$_POST['gid']);
            if(($users = $group->delete()) !== false) {
                $view->createView('delGroupSuccess', array_merge($params, array('users'=>$users, 'groupName'=> $group->name)));
            } else {
                $view->createView('delGroupError', $params);
            }
        } else {
            $view->createView('delGroup', $params);
        }
        return $view;
    }
    
    public function editGroup($params) {
        $view = new administration_View(new Module((int)$params['module']));
        if(isset($_POST['gid']) && isset($_POST['name'])) {
            $group = new Group((int)$_POST['gid']);
            if($group->name != $_POST['name']) {
                $group->name = $_POST['name'];
                $update = true;
            } else {
                $update = false;
            }
            
            if($update) {
                $update = $group->update();
            }
            if($update) {
                $view->createView('editGroupSuccess', $params);
            } else {
                $view->createView('editGroupError', $params);
            }
        } else {
            $view->createView('editGroup', $params);
        }
        return $view;
    }
    
    
    
    //RIGHTS
    
    
    public function userRights($params) {
        $view = new administration_View(new Module((int)$params['module']));
        $view->createView('userRights',$params);
        return $view;
    }
    
    public function groupRights($params) {
        $view = new administration_View(new Module((int)$params['module']));
        $view->createView('groupRights',$params);
        return $view;
    }
}

?>
